<?php
class User_groups_model extends MY_Model {
	private $module = 'user_groups';
	private $table = 'groups';

	function getsearchContent($limit,$page){
		$this->db->select('*');
		$this->db->limit($limit,$page);
		$this->db->order_by($this->input->post('func_order_by'),$this->input->post('order_by'));
		if($this->input->post('content')!='' && $this->input->post('content')!='type here...'){
			$this->db->where('(`name_vi` LIKE "%'.$this->input->post('content').'%" OR `name_en` LIKE "%'.$this->input->post('content').'%" OR `name_gr` LIKE "%'.$this->input->post('content').'%")');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')==''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
		}
		if($this->input->post('dateFrom')=='' && $this->input->post('dateTo')!=''){
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')!=''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		$query = $this->db->get(PREFIX.$this->table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}

	function getTotalsearchContent(){
		$this->db->select('*');
		if($this->input->post('content')!='' && $this->input->post('content')!='type here...'){
			$this->db->where('(`name_vi` LIKE "%'.$this->input->post('content').'%" OR `name_en` LIKE "%'.$this->input->post('content').'%" OR `name_gr` LIKE "%'.$this->input->post('content').'%")');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')==''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
		}
		if($this->input->post('dateFrom')=='' && $this->input->post('dateTo')!=''){
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')!=''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		$query = $this->db->count_all_results(PREFIX.$this->table);

		if($query > 0){
			return $query;
		}else{
			return false;
		}
	}

	function getDetailManagement($id){
		$this->db->select('*');
		$this->db->where('id',$id);
		$query = $this->db->get(PREFIX.$this->table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}

	function saveManagement($fileName=''){
		if($this->input->post('statusAdmincp')=='on'){
			$status = 1;
		}else{
			$status = 0;
		}

		//pr($this->input->post(),1);
		$perms = $this->input->post('permAdmincp');

		//pr($perms,1);
		$upload = '0';
		$download = '0';
		if(!empty($perms)){
			foreach ($perms as $key => $value) {
				if($value == 'download'){
					$download = '1';
				}
				elseif ($value == 'upload') {
					$upload = '1';
				}
			}
		}

		if($this->input->post('hiddenIdAdmincp')==0){
			//Kiểm tra đã tồn tại chưa?
			$checkData = $this->checkData('name_vi',$this->input->post('titleViAdmincp'));
			if($checkData){
				print 'error-title-vi-exists';
				exit;
			}
			$checkData = $this->checkData('name_en',$this->input->post('titleEnAdmincp'));
			if($checkData){
				print 'error-title-en-exists';
				exit;
			}
			$checkData = $this->checkData('name_gr',$this->input->post('titleGrAdmincp'));
			if($checkData){
				print 'error-title-gr-exists';
				exit;
			}

			$data = array(
				'name_vi'=> $this->input->post('titleViAdmincp'),
				'name_en'=> $this->input->post('titleEnAdmincp'),
				'name_gr'=> $this->input->post('titleGrAdmincp'),
				'download'=> $download,
				'upload'=> $upload,
				'status'=> $status,
				'created'=> date('Y-m-d H:i:s',time()),
			);
			if($this->db->insert(PREFIX.$this->table,$data)){
				modules::run('admincp/saveLog',$this->module,$this->db->insert_id(),'Add new','Add new');
				return true;
			}
		}else{
			$result = $this->getDetailManagement($this->input->post('hiddenIdAdmincp'));
			//Kiểm tra đã tồn tại chưa?
			if($result[0]->name_vi!=$this->input->post('titleViAdmincp')){
				$checkData = $this->checkData('name_vi',$this->input->post('titleViAdmincp'));
				if($checkData){
					print 'error-title-vi-exists';
					exit;
				}
			}
			if($result[0]->name_en!=$this->input->post('titleEnAdmincp')){
				$checkData = $this->checkData('name_en',$this->input->post('titleEnAdmincp'));
				if($checkData){
					print 'error-title-en-exists';
					exit;
				}
			}
			if($result[0]->name_gr!=$this->input->post('titleGrAdmincp')){
				$checkData = $this->checkData('name_gr',$this->input->post('titleGrAdmincp'));
				if($checkData){
					print 'error-title-gr-exists';
					exit;
				}
			}

			$data = array(
				'name_vi'=> $this->input->post('titleViAdmincp'),
				'name_en'=> $this->input->post('titleEnAdmincp'),
				'name_gr'=> $this->input->post('titleGrAdmincp'),
				'download'=> $download,
				'upload'=> $upload,
				'status'=> $status,
				'modified'=> date('Y-m-d H:i:s',time())
			);
			modules::run('admincp/saveLog',$this->module,$this->input->post('hiddenIdAdmincp'),'','Update',$result,$data);
			$this->db->where('id',$this->input->post('hiddenIdAdmincp'));
			if($this->db->update(PREFIX.$this->table,$data)){
				return true;
			}
		}
		return false;
	}

	function checkData($field,$title){
		$this->db->select('id');
		$this->db->where($field,$title);
		$this->db->limit(1);
		$query = $this->db->get(PREFIX.$this->table);

		if($query->result()){
			return true;
		}else{
			return false;
		}
	}

	function checkSlug($slug){
		$this->db->select('id');
		$this->db->where('slug',$slug);
		$this->db->limit(1);
		$query = $this->db->get(PREFIX.$this->table);

		if($query->result()){
			return true;
		}else{
			return false;
		}
	}

	/*----------------------FRONTEND----------------------*/
	function getNews($type){
		$this->db->select('*');
		$this->db->where('type',$type);
		$this->db->where('status',1);
		$this->db->order_by('created','DESC');
		$query = $this->db->get(PREFIX.$this->table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}

	function getNewsDetail($slug_detail){
		$this->db->select('id,content');
		$this->db->where('slug',$slug_detail);
		$this->db->where('status',1);
		$this->db->limit(1);
		$query = $this->db->get(PREFIX.$this->table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}
	function checkLogin($user){
		$this->db->select('*');
		$this->db->where('username', $user);
		$this->db->where('status', 1);
		$query = $this->db->get('admin_nqt_users');

		foreach ($query->result() as $row){
			$pass = $row->password;
		}

		if(!empty($pass)){
			return $pass;
		}else{
			return false;
		}
	}
	/*--------------------END FRONTEND--------------------*/
}
